#include "Thread.hh"

int Thread::proc_num = 0;

void
Thread::init(void *(*startRoutine)(void*), void * arg)
{
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  if(pthread_create(&coreThread, &attr, startRoutine, arg)) {
    std::cerr << "pthread_create failed" << std::endl;
    std::exit(1);
  }
   cpu_set_t set;
   CPU_ZERO(&set);
   CPU_SET(proc_num++, &set);
   if(pthread_setaffinity_np(coreThread, sizeof(set), &set) != 0) {
     std::cerr << "Affinity not set: " << strerror(errno) << std::endl;
   }
   //std::cerr << "Setting affinity of thread " << coreThread << " to proc " << proc_num - 1 << "." << std::endl;
  return;
}

void
Thread::kill()
{
  pthread_cancel(coreThread);
  return;
}
